package com.psa.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.psa.element.Customer;
import com.psa.system.R;
import com.psa.tool.SystemStatus;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by david on 14-3-31.
 */
public class CustomerInfoAdapter extends BaseAdapter {
    private Context _context=null;
    private LayoutInflater _inflater = null;
    private ArrayList<HashMap<String, Object>> data = null;
    private boolean ischange = false;

    public CustomerInfoAdapter(Context context, ArrayList<HashMap<String, Object>> data){
        _context = context;
        _inflater = LayoutInflater.from(_context);
        this.data = data;
        ischange = false;
    }

    public CustomerInfoAdapter(Context context, ArrayList<HashMap<String, Object>> data, boolean canChange){
        _context = context;
        _inflater = LayoutInflater.from(_context);
        this.data = data;
        ischange = canChange;
    }
    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    @Override
    public Object getItem(int i) {
        return data==null || i<0 || i>=data.size()?null:data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        try {
            HashMap<String, Object> hashMap = data.get(i);
            if (convertView == null){
                HelperClass helperClass = new HelperClass();
                if (ischange){
                    if (hashMap.get("inputType").toString().equals("0")){
                        convertView = _inflater.inflate(R.layout.list_item_input, null);
                        helperClass.inputType = 0;
                        helperClass.tvTitle = (TextView)convertView.findViewById(R.id.tv_list_item_input);
                        helperClass.etValue = (EditText)convertView.findViewById(R.id.et_list_item_input);
                    } else {
                        convertView = _inflater.inflate(R.layout.list_item_spinner, null);
                        helperClass.inputType = 1;
                        helperClass.tvTitle = (TextView)convertView.findViewById(R.id.tv_list_item_input);
                        helperClass.spinner = (Spinner)convertView.findViewById(R.id.sp_list_item_input);
                        if (hashMap.containsKey("SourceObject")){
                            ArrayAdapter<String> adapter=new ArrayAdapter<String>(_context, android.R.layout.simple_dropdown_item_1line, (String[])hashMap.get("SourceObject"));
//                            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            helperClass.spinner.setAdapter(adapter);
                        }
                    }
                } else {
                    convertView = _inflater.inflate(R.layout.list_item_view, null);
                    helperClass = new HelperClass();
                    helperClass.tvTitle = (TextView)convertView.findViewById(R.id.tv_list_item_key);
                    helperClass.tvValue = (TextView)convertView.findViewById(R.id.tv_list_item_value);
                }
                helperClass.index = i;
                convertView.setTag(helperClass);
            }
            HelperClass helperClass1 = (HelperClass)convertView.getTag();
            helperClass1.tvTitle.setText(hashMap.get("key").toString());
            if (!ischange)
                helperClass1.tvValue.setText(hashMap.get("value")!=null?hashMap.get("value").toString():"");
            else {
                if (helperClass1.inputType == 0 && helperClass1.etValue != null){
                    helperClass1.etValue.setText(hashMap.get("value")!=null?hashMap.get("value").toString():"");
                } else {
                    if (helperClass1.spinner != null && hashMap.containsKey("SourceObject")){
                        String[] slist = (String[])hashMap.get("SourceObject");
                        for (int j = 0; j < slist.length; j++){
                            if (slist[j].equals((hashMap.get("value")!=null?hashMap.get("value").toString():""))){
                                helperClass1.spinner.setSelection(j);
                                break;
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(SystemStatus.TAG, e.getMessage());
        }
        return convertView;
    }

    class HelperClass{
        public TextView tvTitle;
        public TextView tvValue;
        public EditText etValue;
        public Spinner spinner;
        public int inputType;
        public int index;
    }
}
